package cn.admobiletop.adsuyi.adapter.gdt.listener;

import com.qq.e.ads.rewardvideo.RewardVideoAD;
import com.qq.e.ads.rewardvideo.RewardVideoADListener;
import com.qq.e.comm.util.AdError;

import java.util.Map;

import cn.admobiletop.adsuyi.ad.listener.ADSuyiRewardVodAdListener;
import cn.admobiletop.adsuyi.adapter.gdt.data.RewardVodAdInfo;

/**
 * @author ciba
 * @description 激励视频广告监听
 *              需要继承 BaseAdListener<ADSuyiRewardVodAdListener>，实现所适配渠道的开屏广告监听回调，并在回调中调用事先set的adListener，进行相应回调
 *              需要在渠道回调中调用ADSuyi以下方法：
 *                  getAdListener().onAdReceive(this)     广告获取
 *                  getAdListener().onVideoCache(this)    广告已缓存
 *                  getAdListener().onAdExpose(this)      广告曝光
 *                  getAdListener().onReward(this)        广告激励下发
 *                  getAdListener().onAdClose(this)       广告关闭
 *                  getAdListener().onVideoComplete(this) 广告播放完毕
 *                  getAdListener().onAdClick(this)       广告点击
 *                  onAdFailed(code, message)             广告失败
 * @date 2020/4/7
 */
public class RewardVodAdListener extends BaseAdListener<ADSuyiRewardVodAdListener> implements RewardVideoADListener {
    private RewardVideoAD rewardVideoAD;
    private RewardVodAdInfo rewardVodAdInfo;

    public RewardVodAdListener(String platformPosId, ADSuyiRewardVodAdListener adListener) {
        super(platformPosId, adListener);
    }

    /**
     * 设置广点通RewardVideoAD，用于后续的激励视频播放
     *
     * @param rewardVideoAD
     */
    public void setRewardVideoAD(RewardVideoAD rewardVideoAD) {
        this.rewardVideoAD = rewardVideoAD;
    }

    /**
     * 广点通广告获取成功
     */
    @Override
    public void onADLoad() {
        if (getAdListener() != null) {
            // 构造激励视频对象，将对象返回给开发者，用于后续的广告播放
            rewardVodAdInfo = new RewardVodAdInfo(getPlatformPosId());
            // 将rewardVideoAD传入rewardVodAdInfo对象中
            rewardVodAdInfo.setAdapterAdInfo(rewardVideoAD);

            getAdListener().onAdReceive(rewardVodAdInfo);
        }
    }

    /**
     * 广点通视频素材缓存成功
     */
    @Override
    public void onVideoCached() {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onVideoCache(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告页面展示
     */
    @Override
    public void onADShow() {}

    /**
     * 广点通广告曝光
     */
    @Override
    public void onADExpose() {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onAdExpose(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告激励发放
     * @param map
     */
    @Override
    public void onReward(Map<String, Object> map) {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onReward(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告被点击
     */
    @Override
    public void onADClick() {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onAdClick(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告视频素材播放完毕
     */
    @Override
    public void onVideoComplete() {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onVideoComplete(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告被关闭
     */
    @Override
    public void onADClose() {
        if (getAdListener() != null && rewardVodAdInfo != null) {
            getAdListener().onAdClose(rewardVodAdInfo);
        }
    }

    /**
     * 广点通广告流程出错
     * @param adError
     */
    @Override
    public void onError(AdError adError) {
        onAdFailed(adError.getErrorCode(), adError.getErrorMsg());
    }

    /**
     * ADSuyi广告释放会调用该方法
     */
    @Override
    public void release() {
        super.release();
        rewardVideoAD = null;
        if (rewardVodAdInfo != null) {
            rewardVodAdInfo.release();
            rewardVodAdInfo = null;
        }
    }
}
